﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Message.Infrstructre
{
    public class MessageRepsitory<T> : IMessageRepsitory<T> where T : class, new()
    {
        protected readonly EFCoreDbcontext _context;

        public MessageRepsitory(EFCoreDbcontext context)
        {
            _context = context;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        public async Task<int> AddAsync(T a)
        {
            _context.Add(a);
            return await _context.SaveChangesAsync();
        }


        /// <summary>
        /// 获取所有
        /// </summary>
        /// <returns></returns>
        public async Task<List<T>> GetAllAsync()
        {
            return await _context.Set<T>().ToListAsync();
        }
        /// <summary>
        /// 根据id获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<T> GetAllByIdAsync(int id)
        {
            return await _context.Set<T>().FindAsync(id);
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> UpdateAsync(T a)
        {
            _context.Update(a);
            return await _context.SaveChangesAsync();
        }
    }
}
